import axios from '../../http'
import {changeOrdersNum} from '../../../common/bus'

const api = '/api/orders';

const error = (name) => {
  return (e) => console.error(name+':=>',e);
};

export default function save(params, cb, fcb = error('save')) {
  axios.post(api, params).then(d=> {
    changeOrdersNum0();
    cb(d.data.content);
  }).catch(d => fcb(d));
}

export function initOrdersNum() {
  changeOrdersNum0();
}

function countOrdersNum(cb, fcb = error('countOrdersNum')) {
  axios.get(api).then(d => {
    cb(d.data.tabs)
  }).catch(d => fcb(d))
}

function changeOrdersNum0() {
  countOrdersNum(tabs=>{
    let ordersNum = [];
    tabs.forEach(tab=>{
      ordersNum.push(tab.order_form.length)
    });
    changeOrdersNum(ordersNum);
  })
}
